package com.sakila.action.film;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sakila.action.BaseAction;
import com.sakila.action.util.JsonUtil;
import com.sakila.pojo.Film;
import com.sakila.pojo.other.Page;
import com.sakila.service.FilmService;
import com.sakila.vo.FilmVo;

@Controller
@RequestMapping(value={"/", "/staff"})
public class FilmAction implements BaseAction{
	public static final Logger logger = LoggerFactory.getLogger(FilmAction.class);
	@Autowired
	private FilmService filmService;

	@RequestMapping(value="/{path}/goto_viewFilms")
	public String viewFilms(@PathVariable String path){
		switch (path) {
		case "member":
			return "film/film";
		case "staff":
			return "staff/film/film";
		}
		return "staff/index";
	}
	
	@RequestMapping(value="/viewFilmById")
	@ResponseBody
	public Object viewFilmById(Integer filmId){
		List<Film> list = new ArrayList<Film>();
		Film film = filmService.findById(filmId);
		list.add(film);
		return JsonUtil.toJson(list);
	}
	
	@RequestMapping(value="/viewFilm")
	@ResponseBody
	public Object viewList(
			@RequestParam(value=PAGE_ID,defaultValue="1")int pageId,
			@RequestParam(value=PAGE_SIZE,defaultValue="30")int pageSize,
			
			//sorting, easyui will do the request contain toolBar fields at the same time
			@RequestParam(value=SORT_ITEM,required=false)String sortItem,
			@RequestParam(value=ORDER,required=false)String order,
			
			//for toolBar searching, when sorting, easyui will do the request contain these fields at the same time
			@RequestParam(required=false)String titleName,
			@RequestParam(required=false)String actorName,
			@RequestParam(required=false)Integer categoryId){
		logger.debug(">>>>>>>>>>>>>>>>" + titleName);
		Page page = new Page(pageId, pageSize);
		FilmVo filmVo;
		if(sortItem != null && order !=null){
			filmVo = getFilmVoFromServiceSorted(page, sortItem, order, titleName, actorName, categoryId); 
		}else {
			filmVo = getFilmVoFromService(page, titleName, actorName, categoryId);
		}
		return JsonUtil.toJson(filmVo.getList(), filmVo.getTotalNumOfRows());
	}
	
	private FilmVo getFilmVoFromService(Page page, String titleName, String actorName, Integer categoryId){
		FilmVo filmVo;
		if (titleName != null || actorName != null || categoryId != null) {
			filmVo = filmService.findByTitleOrActorOrCategory(page, titleName, actorName, categoryId);
		}else {
			filmVo = filmService.findAll(page);
		}
		return filmVo;
	}
	
	private FilmVo getFilmVoFromServiceSorted(Page page, String sortItem, String order, 
											String titleName, String actorName, Integer categoryId){
		FilmVo filmVo;
		if (titleName != null || actorName != null || categoryId != null) {
			filmVo = filmService.sortFilms(page, order, sortItem, titleName, actorName, categoryId);
		}else {
			filmVo = filmService.sortFilms(page, order, sortItem);
		}
		return filmVo;
	}
}
